/**
 * 时间：2024-08-25
 * 作者：waygc
 * 描述：这个文件是模拟从服务器获得数据，提交数据的过程，并且定义、保存假数据
 * 
 */
import utils from "./../../lib/utils";

let fakeData = [
	{
		name: '骨科康复——足底筋膜康复',
		nameType: 0,
		type: 0,
		fileType: 1,
		target: 0,
		createTime: '2024-02-16 10:10:10',
		classType: 0,
		count: 32,
		picture: "https://img.redocn.com/sheying/20220722/fengjinghua_12530187.jpg.400.jpg",
		files: "第一个文件.ppt",
		key: "1590489471753.322"
	},
	{
		name: '骨科康复——半月板康复',
		nameType: 0,
		type: 1,
		fileType: 0,
		target: 2,
		createTime: '2024-03-16 10:10:10',
		classType: 0,
		count: 20,
		picture: "https://gw.alipayobjects.com/zos/antfincdn/LlvErxo8H9/photo-1503185912284-5271ff81b9a8.webp",
		files: "第二个文件.ppt,补充说明文件.ppt",
		key: "1590483338593.32522"
	},
	{
		name: '骨科康复——膝关节康复',
		nameType: 0,
		type: 1,
		fileType: 1,
		target: 1,
		createTime: '2024-02-26 20:10:10',
		classType: 0,
		count: 15,
		picture: "https://img95.699pic.com/photo/60020/9453.jpg_wh300.jpg",
		files: "第一个文件.ppt",
		key: "1590489446593.41522"
	},
	{
		name: '骨科康复——腿骨骨折康复',
		nameType: 0,
		type: 1,
		fileType: 1,
		target: 1,
		createTime: '2024-04-06 10:33:10',
		classType: 0,
		count: 15,
		picture: "https://img.zcool.cn/community/01e76d5c93682da8012141680d1341.jpg@2o.jpg",
		key: "1590489477593.522"
	},
	{
		name: '心肺康复——肺部手术',
		nameType: 2,
		type: 1,
		fileType: 0,
		target: 1,
		createTime: '2024-03-11 11:11:12',
		classType: 0,
		count: 15,
		picture: "https://img.zcool.cn/community/01c2905a42da98a801206ed36192f2.jpg@2o.jpg",
		key: "1590489466593.5122"
	},
	{
		name: '骨科康复——肌腱康复',
		nameType: 0,
		type: 1,
		fileType: 1,
		target: 1,
		createTime: '2024-04-30 10:15:15',
		classType: 0,
		count: 15,
		picture: "https://img.zcool.cn/community/01fcaf5b72819ca801215c8f30c6a3.jpg@2o.jpg",
		key: "1500009488593.1522"
	},
	{
		name: '五官科康复——视力康复',
		nameType: 1,
		type: 1,
		fileType: 1,
		target: 1,
		createTime: '2024-04-26 10:30:10',
		classType: 2,
		count: 15,
		picture: "https://img.zcool.cn/community/01e5a95680f9ab32f8759f04f85a36.jpg@1280w_1l_2o_100sh.jpg",
		key: "1545489488593.522"
	},
	{
		name: '五官科康复——听力康复',
		nameType: 1,
		type: 1,
		fileType: 0,
		target: 1,
		createTime: '2024-04-26 10:30:10',
		classType: 2,
		count: 15,
		picture: "https://www.chinayis.com/image.php?maxwidth=1550&maxheight=1550&image=/Uploads/Picture/2018-04-14/5ad1531b0cc29.jpeg",
		key: "15904894454588.593"
	},
	{
		name: '骨科康复——肘部骨折康复',
		nameType: 0,
		type: 1,
		fileType: 1,
		target: 1,
		createTime: '2024-01-10 13:10:10',
		classType: 2,
		count: 15,
		picture: "https://ts1.cn.mm.bing.net/th/id/R-C.ac06c569db94f6b3bb5a97ff7ad60455?rik=KllYEZjV3JQP9g&riu=http%3a%2f%2fwww.youhuas.com%2fyouhua%2fimage%2f20140623144209470.jpg&ehk=YKRdbApXB%2b3vDXA1KPUuITv%2bm1KXWfvRtB7pRBo7veE%3d&risl=&pid=ImgRaw&r=0",
		key: "1590489488593.2200"
	},
	{
		name: '五官科康复——鼻炎康复',
		nameType: 1,
		type: 1,
		fileType: 1,
		target: 1,
		createTime: '2024-08-16 14:10:21',
		classType: 2,
		count: 15,
		picture: "https://img.zcool.cn/community/01747d5a3db315a8012197413e4f05.jpg@3000w_1l_2o_100sh.jpg",
		key: "1590489488593.4223"
	},
	{
		name: '五官科康复——兔唇康复',
		nameType: 1,
		type: 1,
		fileType: 1,
		target: 1,
		createTime: '2024-04-16 10:10:30',
		classType: 2,
		count: 15,
		picture: "https://img.zcool.cn/community/0115f255bcb6476ac7253f360abc28.jpg@2o.jpg",
		key: "1590489468593.522"
	},
	{
		name: '心肺康复——心脏手术',
		nameType: 2,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2024-05-14 10:20:15',
		classType: 3,
		count: 15,
		picture: "https://img.zcool.cn/community/019c785b503cd0a80121ade0845480.jpg@1280w_1l_2o_100sh.jpg",
		key: "1590475488593.522"
	},
	{
		name: '高血压专题——中风康复',
		nameType: 3,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2024-08-25 10:20:10',
		classType: 3,
		count: 15,
		picture: "https://img.zcool.cn/community/01618e5d6e8148a801202f1756fb43.jpg@3000w_1l_2o_100sh.jpg",
		key: "15922489488593.522"
	},
	{
		name: '糖尿病专题——糖尿病的需知',
		nameType: 3,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2023-04-14 10:20:10',
		classType: 1,
		count: 2,
		picture: "https://img.zcool.cn/community/01386b596eab57a8012193a3549101.jpg@1280w_1l_2o_100sh.jpg",
		key: "1590489488593.4012"
	},
	{
		name: '糖尿病专题——糖尿病的自查',
		nameType: 3,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2023-04-14 10:20:10',
		classType: 1,
		count: 2,
		picture: "https://img.zcool.cn/community/01bc8956dda3416ac72531cb1c6155.jpg@3000w_1l_0o_100sh.jpg",
		key: "1590489488593.519"
	},
	{
		name: '糖尿病专题——糖尿病高危人群',
		nameType: 3,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2023-04-14 10:20:10',
		classType: 1,
		count: 2,
		picture: "https://img.zcool.cn/community/01a9c85b3a6dd1a80121b99445a8d5.jpeg@3000w_1l_0o_100sh.png",
		key: "1590489488503.522"
	},
	{
		name: '糖尿病专题——糖尿病常见初期症状',
		nameType: 3,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2023-04-14 10:20:10',
		classType: 1,
		count: 2,
		picture: "https://ts1.cn.mm.bing.net/th/id/R-C.53fb133cf23e306b4bb3e83f046cd944?rik=bLQyDZYBMcIixg&riu=http%3a%2f%2fimg14.artimg.net%2fgallery%2f2017%2f0930%2f150677348711336750.jpg&ehk=OA6cH8za2QxID6rMMtx3hqUr7wdUp%2bZRXmN2hydaugY%3d&risl=&pid=ImgRaw&r=0",
		key: "15904822488593.522"
	},
	{
		name: '糖尿病专题——糖尿病饮食营养结构',
		nameType: 3,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2023-04-14 10:20:10',
		classType: 1,
		count: 2,
		picture: "https://img.zcool.cn/community/01e1d25680f9a96ac7251bb64cbffd.jpg@2o.jpg",
		key: "1590489488591.522"
	},
	{
		name: '糖尿病专题——糖尿病及其并发症',
		nameType: 3,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2023-04-14 10:20:10',
		classType: 1,
		count: 2,
		picture: "https://img.zcool.cn/community/012dac57f5be57a84a0d304fd6710a.jpg@1280w_1l_2o_100sh.jpg",
		key: "1590489488596.522"
	},
	{
		name: '高血压专题——高血压常见情况',
		nameType: 3,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2023-04-14 10:20:10',
		classType: 1,
		count: 2,
		picture: "https://img.zcool.cn/community/015e8759532655a8012193a32955b4.jpg@3000w_1l_0o_100sh.jpg",
		key: "1590489468593.502"
	},
	{
		name: '高血压专题——饮食营养结构',
		nameType: 3,
		type: 1,
		fileType: 1,
		target: 0,
		createTime: '2023-04-14 10:20:10',
		classType: 1,
		count: 2,
		picture: "https://img.zcool.cn/community/0176455db91048a8012163baf5eb38.jpg@3000w_1l_2o_100sh.jpg",
		key: "1590489488593.521"
	},
	{
		name: '其他慢性病——其他慢性病介绍',
		nameType: 3,
		type: 1,
		fileType: 0,
		target: 0,
		createTime: '2023-04-14 10:20:10',
		classType: 0,
		count: 2,
		picture: "https://img.art138.com/merchant/2019/04/23/wGYEs706Y250WuJJ7omiVx0X.jpeg",
		key: "1590489488593.512"
	},
];

for (const obj of fakeData) {
	obj.publish = 0;
	obj.readTime = Math.round(300 * Math.random());
}

function seletByLikeName (value, name) {
	return value.indexOf(name) === 0;
}

export default {
	getList (searchObj) {
		let start = (searchObj.page - 1) * searchObj.size;
		let end = searchObj.page * searchObj.size;

		// 此处用于模拟获得数据的过程
		let list = fakeData.filter(obj => {
			const hasNameType = utils.checkObjectIsNotEmpty(searchObj.name);
			const hastgtType = utils.checkNumberIsNotEmpty(searchObj.tgtType);
			const hasClassType = utils.checkNumberIsNotEmpty(searchObj.classType);
			// 至少有一个选项
			if (hasNameType && hastgtType && hasClassType) {
				return obj.target === searchObj.tgtType
					&& obj.classType === searchObj.classType
					&& seletByLikeName(obj.name, searchObj.name)
			} else if (hasNameType && hastgtType) {
				return obj.target === searchObj.tgtType
					&& seletByLikeName(obj.name, searchObj.name)
			} else if (hasNameType && hasClassType) {
				return obj.classType === searchObj.classType
					&& seletByLikeName(obj.name, searchObj.name);
			} else if (hastgtType && hasClassType) {
				return obj.target === searchObj.tgtType
					&& obj.classType === searchObj.classType;
			} else if (hasNameType) {
				return seletByLikeName(obj.name, searchObj.name);
			} else if (hastgtType) {
				return obj.target === searchObj.tgtType;
			} else if (hasClassType) {
				return obj.classType === searchObj.classType;
			} else {
				// 没有选项
				return true;
			}
		});

		const total = list.length;
		list = list.slice(start, end);

		const output = {
			data: list,
			paging: {
				page: searchObj.page,
				size: searchObj.size,
				total: total
			}
		};

		return new Promise((resolve, reject) => {
			setTimeout(() => {
				resolve(output);
			}, 1000);
		});
	},
	getDetail (id) {
		for (const obj of fakeData) {
			if (obj.key === id) {
				return {
					success: true,
					data: obj
				}
			}
		}

		return {
			success: false
		}
	},
	del (id) {
		const output = fakeData.filter(obj => {
			return obj.key !== id;
		});

		fakeData = output;

		return {
			success: true
		}
	},
	edit (id, data) {
		const output = fakeData.map(obj => {
			if (obj.key === id) {
				return data;
			} else {
				return obj;
			}
		});

		fakeData = output;

		return {
			success: true
		}
	},
	save (data) {

		const key = data.key = Date.now() * Math.random();

		fakeData.push(data);

		return {
			success: true,
			id: key
		}
	},
	removeUploadFile (rowId, fileName) {
		const data = fakeData.filter(obj => {
			return obj.key === rowId;
		});

		if (data[0]) {
			data[0].picture = "";
		}
	}
}
